package com.btshop.goods.service.impl;

import com.btshop.goods.mapper.BrandMapper;
import com.btshop.goods.pojo.Brand;
import com.btshop.goods.pojo.BrandExample;
import com.btshop.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.util.IdWorker;
import com.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName BrandServiceImpl
 * @Description 品牌业务接口实现层
 * @Author by nike
 * @Date 2020/7/13 0:25
 * @Version V1.0
 **/
@Slf4j
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BrandMapper brandMapper;

    /**
     * @Description 查询所有品牌
     * @Date 2020/7/14 22:31
     * @Param []
     * @return java.util.List<com.btshop.goods.pojo.Brand>
     * @Author ld
     **/
    @Override
    public List<Brand> findAll() {
        BrandExample example = new BrandExample();
        List<Brand> brands = brandMapper.selectByExample(example);
        return brands;
    }

    /**
     * @Description 根据id查询品牌
     * @Date 2020/7/14 22:30
     * @Param [id]
     * @return com.btshop.goods.pojo.Brand
     * @Author ld
     **/
    @Override
    public Brand findById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    /**
     * @Description 品牌新增
     * @Date 2020/7/14 22:32
     * @Param [brand]
     * @return int
     * @Author ld
     **/
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int add(Brand brand) {
        brand.setId(idWorker.nextId());
        return brandMapper.insertSelective(brand);
    }

    /**
     * @description: 根据id更新品牌
     * @Param: [id, brand]
     * @return: int
     * @author: 李东
     * @time: 2020-7-15 11:02
     **/
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int update(Long id, Brand brand) {
        brand.setId(id);
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * @description: 根据id删除品牌
     * @Param: [id]
     * @return: int
     * @author: 李东
     * @time: 2020-7-15 17:28
     **/
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int delete(Long id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * @description: 根据品牌信息多条件查询
     * @Param: [brand]
     * @return: java.util.List<com.btshop.goods.pojo.Brand>
     * @author: 李东
     * @time: 2020-7-15 17:36
     **/
    @Override
    public List<Brand> search(Brand brand) {
        BrandExample example = buildExample(brand);
        return  brandMapper.selectByExample(example);
    }

    /**
     * @description: 构建查询条件
     * @Param: [brand]
     * @return: com.btshop.goods.pojo.BrandExample
     * @author: 李东
     * @time: 2020-7-15 17:46
     **/
    public BrandExample buildExample(Brand brand) {
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        if(null != brand){
            if(!StringUtils.isEmpty(brand.getName())){
                criteria.andNameLike("%"+brand.getName()+"%");
            }
            if(!StringUtils.isEmpty(brand.getLetter())){
                criteria.andLetterEqualTo(brand.getLetter());
            }
        }
        return example;
    }

    /**
     * @description: 根据品牌信息多条件分页查询
     * @Param: [page, size, brand]
     * @return: com.github.pagehelper.PageInfo<com.btshop.goods.pojo.Brand>
     * @author: 李东
     * @time: 2020-7-15 17:56
     **/
    @Override
    public PageInfo<Brand> searchPage(Integer page, Integer size, Brand brand) {
        BrandExample example = buildExample(brand);
        PageHelper.startPage(page,size);
        List<Brand> brands = brandMapper.selectByExample(example);

        return new PageInfo<Brand>(brands);
    }
}
